import axios from '../../utils/request'
import localStorage from "../../utils/MyLocalStorage";
import api from "../../api";
const moduleURL = '/sport'
const state = {
  studentUser:localStorage.get('student_user')|| {},
}

//方法参考地址
//https://www.jb51.net/article/261401.htm
const mutations = {

  setStudentUser: (state, data) => {
    state.studentUser = data
  },

}
const actions = {

  /**
   * 获取分页数据
   * @param commit
   * @param params
   * @returns {Promise<unknown>}
   */
  getListPageData: function ({commit}, params) {

    return new Promise((resolve, reject) => {
      // const params = {page:1,pageSize:1}; // 参数
      axios.get(moduleURL+'/listPage',{params:params}).then((res) => {
          // console.log('find ' +JSON.stringify(res))
        resolve(res.data)
        }).catch((error) => {
          reject(error)
        })

    }).catch(error=>{
      console.log(error)
    })

  },


  /**
   * 通过id获取信息
   * @param commit
   * @param params
   * @returns {Promise<unknown>}
   */
  findDataById: function ({commit}, id) {
    return new Promise((resolve, reject) => {
      const params = {id:id}; // 参数
      // console.log("id "+id);
      axios.get(moduleURL+'/findDetail',{params:params}).then((res) => {
        let studentUser = res.data
        // console.log('res.data ' +JSON.stringify(studentUser))
        localStorage.set("student_user",studentUser,60*60*4)//4个小时
        commit("setStudentUser", studentUser)//保存用户数据
        resolve(res)
      }).catch((error) => {
        reject(error)
      })
    }).catch(error=>{
      console.log(error)
    })

  },

  /**
   * 更新体校学生数据
   * @param commit
   * @param params
   * @returns {Promise<unknown>}
   */
  updateObject: function ({commit}, from) {
    return new Promise((resolve, reject) => {
      // console.log('from ' +JSON.stringify(from))
      axios.post(moduleURL+'/saveOrUpdate', from).then((res) => {
        resolve(res)
      }).catch((error) => {
        reject(error)
      })

    }).catch(error=>{
      console.log(error)
    })

  },

  /**
   * 删除体校学生数据
   * @param commit
   * @param params
   * @returns {Promise<unknown>}
   */
  deleteObject: function ({commit}, ids) {
    return new Promise((resolve, reject) => {
      axios.delete(moduleURL+'/deleteByIds?ids='+ids).then((res) => {
        resolve(res)
      }).catch((error) => {
        reject(error)
      })
    }).catch(error=>{
      console.log(error)
    })

  },


}


export default {
  namespaced: true,
  state,
  mutations,
  actions
}
